package com.jcj.endecoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 自定义长度解码器LengthFieldBasedFrameDecoder对发送接收数据的处理
 */
@ChannelHandler.Sharable
public class HandlerServerLengthFieldBased extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        //读取消息,并按照自定义的协议格式进行消息处理
        ByteBuf in = (ByteBuf) msg;

        //打印报文
        String srcInfo = ByteBufUtil.hexDump(in).toUpperCase();
        System.out.println("收到的原始报文:" + srcInfo);

        //进行报文解析
        short header = in.readShort();
        byte msgtype = in.readByte();
        int contentLen = in.readInt();
        ByteBuf bufContent = in.readBytes(contentLen);
        String content = bufContent.toString(CharsetUtil.UTF_8);

        //输出传输的消息
        System.out.println("收到的消息为:" + content);

         //给客户端反馈信息
        short header2 = 0X3C3C;
        byte[] msgtype2 = new byte[]{0x01};
        String strContent2 = "你好,宝塔镇河妖!";
        int contentLen2 = strContent2.getBytes().length;//内容的字节数

        //写入通道
        ctx.write(Unpooled.copyShort(header2));
        ctx.write(Unpooled.copiedBuffer(msgtype2));
        ctx.write(Unpooled.copyInt(contentLen2));
        ctx.write(Unpooled.copiedBuffer(strContent2, CharsetUtil.UTF_8));

        //清空缓冲区,发送消息
        ctx.flush();

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //出现异常的时候执行的动作(打印并关闭通道)
        cause.printStackTrace();
        ctx.close();
    }

    /**
     * 新建立连接时触发的动作
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        //System.out.println("打印channel:"+channel);
        //System.out.println("运行类型:"+channel.getClass());
        System.out.println("客户端: " + channel.remoteAddress() + "已连接上来");
    }

    /**
     * 连接断开时触发的动作
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println("客户端: " + channel.remoteAddress() + "已断开");
    }

    /**
     * 通道处于活动状态触发的动作,该方法只会在通道建立时调用一次
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println("客户端: " + channel.remoteAddress() + "在线");
    }

    /**
     * 通道处于非活动状态触发的动作,该方法只会在通道失效时调用一次
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println("客户端: " + channel.remoteAddress() + "掉线");
    }
}
